//
//  WHYNetworkTools.m
//  WHYWeibo-OC
//
//  Created by 王洪运 on 15/11/24.
//  Copyright © 2015年 王洪运. All rights reserved.
//

#import "WHYNetworkTools.h"


@interface WHYNetworkTools ()

@property (copy, nonatomic) NSString *appKey;
@property (copy, nonatomic) NSString *appAecrect;
@property (copy, nonatomic) NSString *redirectUri;



@end

@implementation WHYNetworkTools
#pragma mark - 懒加载
-(NSString *)redirectUri {
    if (_redirectUri == nil) {
        _redirectUri = @"http://www.baidu.com";
    }
    return _redirectUri;
}

-(NSString *)appAecrect {
    if (_appAecrect == nil) {
        _appAecrect = @"37ee81b336ae189998234f88ab99d7ca";
    }
    return _appAecrect;
}

-(NSString *)appKey {
    if (_appKey == nil) {
        _appKey = @"4153274476";
    }
    return _appKey;
}

-(NSURL *)oauthURL {
    if (_oauthURL == nil) {
        NSString *str = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",self.appKey,self.redirectUri];
        _oauthURL = [NSURL URLWithString:str];
    }
    return _oauthURL;
}

#pragma mark - sharedTools
+(instancetype)sharedTools {
    static WHYNetworkTools *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithBaseURL:nil];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
        
    });
    
    return instance;
}

#pragma mark - 加载访问令牌
-(void)loadAccessTokenWithCode:(NSString *)code finished:(WHYRequestCallBack)finished {
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *parameter = @{@"client_id": self.appKey,
                                                             @"client_secret": self.appAecrect,
                                                             @"grant_type": @"authorization_code",
                                                             @"code": code,
                                                             @"redirect_uri":self.redirectUri,};
    
    [self requestWithHTTPMethod:POST urlString:urlString parameters:parameter finished:finished];
    
}

#pragma mark - 请求网络
-(void)requestWithHTTPMethod:(WHYRequestMethod)method
                   urlString:(NSString *)urlString
                   parameters:(NSDictionary *)parameters
                   finished:(WHYRequestCallBack)finished
{

    void (^success)(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        finished(task, nil);
    };
    
    void(^failure)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        finished(nil, error);
    };
    
    if (method == GET) {
        [[WHYNetworkTools sharedTools] GET:urlString parameters:parameters success:success failure:failure];
    }else if (method == POST) {
        [[WHYNetworkTools sharedTools] POST:urlString parameters:parameters success:success failure:failure];
    }
    
    
}



@end
